/*
 * @lc app=leetcode.cn id=151 lang=cpp
 *
 * [151] 翻转字符串里的单词
 */

// @lc code=start
class Solution {
public:
    string reverseWords(string s) {

        vector<string> words;
        int i = 0;
        //找到每一个单词
        while(i < s.size()){
            //找到第一个合法字符
            while(i < s.size()) if(s[i] != ' ') {break;} else i++;
            string word;
            while(i < s.size() && s[i] != ' '){
                word += s[i];
                i++;
            }
            if(word.size() != 0)   words.push_back(word);
        }

        string ans;

        if(words.size() == 0) return ans;

        for(int i = words.size() - 1; i >= 0; i--){
            ans += words[i] + " ";
        }
        ans.pop_back();

        return ans;
    }
};
// @lc code=end

